<?php

/**
 * User: zhangligang
 * Email: tzhang2450@163.com
 * Date: 2018/7/22
 * Time: 上午10:27
 */

namespace App\Http\Controllers\V1;

use App\Http\Controllers\Controller;
use App\Models\User;
use App\Providers\MyJwt;
use App\Services\ApartService;
use App\Services\ContractService;
use App\Services\FeeService;
use App\Services\NoticeService;
use App\Services\ProgramService;
use App\Services\RentService;
use App\Services\RepairService;
use App\Services\RetireService;
use App\Services\Service;
use App\Services\UserService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;

class ManagerController extends Controller
{

    private $uid;

    public function __construct(Request $request)
    {
        parent::__construct();
        $authorization = $request->header('Authorization');
        $jwt = new MyJwt();
        $jwtCode = $jwt->decode($authorization, MyJwt::ENCODE_KEY);
        if (!$jwtCode || !array_key_exists('uid', $jwtCode)) {
            $this->sendJson([], 4031, '请登录');
        }
        if (!array_key_exists('role', $jwtCode) || $jwtCode['role'] != 5) {
            $this->sendJson([], 4032, '非法请求');
        }
        if (!array_key_exists('timeout', $jwtCode) || $jwtCode['timeout'] < time()) {
            $this->sendJson([], 4031, '请登录');
        }

        $cacheKey = sprintf('ht:login:uid:%d', $jwtCode['uid']);
        $cacheData = Redis::get($cacheKey);
        if (!$cacheData || $cacheData < time()) {
            Redis::setex($cacheKey, 0, null);
            $this->sendJson([], 4031, '请登录');
        }
        $this->jwtCode = $jwtCode;
        $this->uid = $jwtCode['uid'];
    }

    public function apartStandard(Request $request)
    {
        $this->sendGet(function() use($request) {
            $this->sendJson([]);
        });
    }

    public function noticeList(Request $request)
    {
        $this->sendGet(function() use($request) {
            $limit = $request->get('limit');
            $offset = $request->get('offset');
            $order = $request->get('order');
            $data = NoticeService::getNoticeList($limit, $offset, $order);
            $this->sendDataTable($data);
        });
    }

    public function noticeDetail(Request $request)
    {
        $this->sendGet(function() use($request) {
            $id = $request->get('id');
            $data = NoticeService::getNoticeDetail($id);
            $this->sendJson($data);
        });
    }

    public function noticeSave(Request $request)
    {
        $this->sendPost(function() use($request) {
            $data = $request->get('data');
            NoticeService::saveNotice($data);
            $this->sendJson([], 200, '保存成功');
        });
    }

    public function noticeUpdate(Request $request)
    {
        $this->sendPost(function() use($request) {
            $data = $request->get('data');
            NoticeService::saveNotice($data);
            $this->sendJson([], 200, '更新成功');
        });
    }

    public function noticeDelete(Request $request)
    {
        $this->sendPost(function() use($request) {
            $id = $request->get('id');
            NoticeService::deleteNotice($id);
            $this->sendJson([], 200, '删除成功');
        });
    }

    public function apartList(Request $request)
    {
        $this->sendGet(function() use($request) {
            $limit = $request->get('limit');
            $offset = $request->get('offset');
            $order = $request->get('order');
            $search = $request->get('search');
            $region = $request->get('region');
            $status = $request->get('status');
            $option = [];
            if ($search) {
                $option['query'][] = [
                    'key' => 'name',
                    'operator' => 'like',
                    'value' => "%{$search}%"
                ];
            }
            if ($region) {
                $option['query'][] = [
                    'key' => 'region',
                    'operator' => '=',
                    'value' => $region
                ];
            }
            if ($status) {
                $option['query'][] = [
                    'key' => 'status',
                    'operator' => '=',
                    'value' => $status
                ];
            }
            $data = ApartService::getApartList($limit, $offset, $order, $option);

            $this->sendDataTable($data);
        });
    }

    public function apartDetail(Request $request)
    {
        $this->sendGet(function() use($request) {
            $id = $request->get('id');
            $data = ApartService::getApartDetail($id);

            $this->sendJson($data);
        });
    }

    public function apartSave(Request $request)
    {
        $this->sendPost(function() use($request) {
            $data = $request->get('data');
            ApartService::saveApart($data);
            $this->sendJson([], 200, '保存成功');
        });
    }

    public function apartUpdate(Request $request)
    {
        $this->sendPost(function() use($request) {
            $data = $request->get('data');
            ApartService::saveApart($data);
            $this->sendJson([], 200, '更新成功');
        });
    }

    public function apartDelete(Request $request)
    {
        $this->sendPost(function() use($request) {
            $id = $request->get('id');
            ApartService::deleteApart($id);
            $this->sendJson([], 200, '删除成功');
        });
    }

    public function regionList(Request $request)
    {
        $this->sendGet(function() use($request) {
            $limit = $request->get('limit');
            $offset = $request->get('offset');
            $order = $request->get('order');
            $data = Service::getRegionList($limit, $offset, $order);
            $this->sendDataTable($data);
        });
    }

    public function regionDetail(Request $request)
    {
        $this->sendGet(function() use($request) {
            $id = $request->get('id');
            $data = Service::getRegionDetail($id);
            $this->sendJson($data);
        });
    }

    public function regionSave(Request $request)
    {
        $this->sendPost(function() use($request) {
            $data = $request->get('data');
            Service::saveRegion($data);
            $this->sendJson([], 200, '保存成功');
        });
    }

    public function regionUpdate(Request $request)
    {
        $this->sendPost(function() use($request) {
            $data = $request->get('data');
            Service::saveRegion($data);
            $this->sendJson([], 200, '更新成功');
        });
    }

    public function regionDelete(Request $request)
    {
        $this->sendPost(function() use($request) {
            $id = $request->get('id');
            Service::deleteRegion($id);
            $this->sendJson([], 200, '删除成功');
        });
    }

    public function standardList(Request $request)
    {
        $this->sendGet(function() use($request) {
            $limit = $request->get('limit');
            $offset = $request->get('offset');
            $order = $request->get('order');
            $data = Service::getStandardList($limit, $offset, $order);
            $this->sendDataTable($data);
        });
    }

    public function standardDetail(Request $request)
    {
        $this->sendGet(function() use($request) {
            $id = $request->get('id');
            $data = Service::getStandardDetail($id);
            $this->sendJson($data);
        });
    }

    public function standardSave(Request $request)
    {
        $this->sendPost(function() use($request) {
            $data = $request->get('data');
            Service::saveStandard($data);
            $this->sendJson([], 200, '保存成功');
        });
    }

    public function standardUpdate(Request $request)
    {
        $this->sendPost(function() use($request) {
            $data = $request->get('data');
            Service::saveStandard($data);
            $this->sendJson([], 200, '更新成功');
        });
    }

    public function standardDelete(Request $request)
    {
        $this->sendPost(function() use($request) {
            $id = $request->get('id');
            Service::deleteStandard($id);
            $this->sendJson([], 200, '删除成功');
        });
    }

    public function programList(Request $request)
    {
        $this->sendGet(function() use($request){
            $limit = $request->get('limit');
            $offset = $request->get('offset');
            $order = $request->get('order');
            $search = $request->get('search');
            $state = $request->get('state');
            $option = [];
            if ($search) {
                $option['query'][] = [
                    'key' => 'title',
                    'operator' => 'like',
                    'value' => "%{$search}%"
                ];
            }
            if ($state) {
                $option['query'][] = [
                    'key' => 'state',
                    'operator' => '=',
                    'value' => $state - 1
                ];
            }
            $data = ProgramService::getProgramList($limit, $offset, $order, $option);
            $this->sendDataTable($data);
        });
    }

    public function programDetail(Request $request)
    {
        $this->sendGet(function() use($request) {
            $id = $request->get('id');
            $data = ProgramService::getProgramDetail($id);
            $this->sendJson($data);
        });
    }

    public function programSave(Request $request)
    {
        $this->sendPost(function() use($request){
            $data = $request->get('data');

            $startEndTime = explode(' ~ ', $data['start_end_time']);
            $applyStartEndTime = explode(' ~ ', $data['apply_start_end_time']);
            $checkStartEndTime = explode(' ~ ', $data['check_start_end_time']);

            ProgramService::saveProgram([
                'title' => $data['title'],
                'start_at' => $startEndTime[0],
                'end_at' => $startEndTime[1],
                'apply_start_at' => $applyStartEndTime[0],
                'apply_end_at' => $applyStartEndTime[1],
                'check_start_at' => $checkStartEndTime[0],
                'check_end_at' => $checkStartEndTime[1],
                'desc' => $data['desc'],
                'state' => $data['state'],
                'status' => $data['status']
            ]);
            $this->sendJson([], 200, '保存成功');
        });
    }

    public function programUpdate(Request $request)
    {
        $this->sendPost(function() use($request){
            $data = $request->get('data');

            $startEndTime = explode(' ~ ', $data['start_end_time']);
            $applyStartEndTime = explode(' ~ ', $data['apply_start_end_time']);
            $checkStartEndTime = explode(' ~ ', $data['check_start_end_time']);

            ProgramService::saveProgram([
                'id' => $data['id'],
                'title' => $data['title'],
                'start_at' => $startEndTime[0],
                'end_at' => $startEndTime[1],
                'apply_start_at' => $applyStartEndTime[0],
                'apply_end_at' => $applyStartEndTime[1],
                'check_start_at' => $checkStartEndTime[0],
                'check_end_at' => $checkStartEndTime[1],
                'desc' => $data['desc'],
                'state' => $data['state'],
                'status' => $data['status']
            ]);
            $this->sendJson([], 200, '更新成功');
        });
    }

    public function programDelete(Request $request)
    {
        $this->sendPost(function() use($request) {
            $id = $request->get('id');
            ProgramService::deleteProgram($id);
            $this->sendJson([], 200, '删除成功');
        });
    }

    public function addProgramAparts(Request $request)
    {
        $this->sendPost(function() use($request) {
            $id = $request->get('id');
            $data = $request->get('data');
            ProgramService::addProgramAparts($id, $data);
            $this->sendJson([], 200, '添加成功');
        });
    }

    public function unselectedAparts(Request $request)
    {
        $this->sendGet(function() use($request) {
            $programId = $request->get('program_id');
            $data = ApartService::getUnselectAparts($programId);
            $this->sendJson($data);
        });
    }

    public function programApartList(Request $request, $programId)
    {
        $this->sendGet(function() use($request, $programId) {
            $limit = $request->get('limit');
            $offset = $request->get('offset');
            $order = $request->get('order');
            $data = ApartService::getProgramApartList($programId, $limit, $offset, $order);
            $this->sendDataTable($data);
        });
    }

    public function deleteProgramApart(Request $request)
    {
        $this->sendPost(function() use($request) {
            $id = $request->get('id');
            ApartService::deleteProgramApart($id);
            $this->sendJson([], 200, "删除成功");
        });
    }

    public function programUserList(Request $request, $programId)
    {
        $this->sendGet(function() use($request, $programId) {
            $limit = $request->get('limit');
            $offset = $request->get('offset');
            $order = $request->get('order');
            $data = ProgramService::getProgramUserList($programId, $limit, $offset, $order);
            $this->sendDataTable($data);
        });
    }

    public function changeProgramUserStatus(Request $request)
    {
        $this->sendPost(function() use($request) {
            $data = $request->get('data');
            ProgramService::changeProgramUserStatus($data);
            $this->sendJson([], 200, "修改成功");
        });
    }

    public function programUserApartList(Request $request, $programId)
    {
        $this->sendGet(function() use($request, $programId) {
            $limit = $request->get('limit');
            $offset = $request->get('offset');
            $order = $request->get('order');
            $data = ProgramService::getProgramUserApartList($programId, $limit, $offset, $order);
            $this->sendDataTable($data);
        });
    }

    public function changeUserApartStatus(Request $request)
    {
        $this->sendPost(function() use($request){
            $data = $request->get('data');
            ApartService::changeUserApartStatus($data);
            $this->sendJson([], 200, '操作成功');
        });
    }

    public function contractList(Request $request)
    {
        $this->sendGet(function() use($request){
            $limit = $request->get('limit');
            $offset = $request->get('offset');
            $order = $request->get('order');
            $search = $request->get('search');
            $process = $request->get('process');
            $option = [];
            if ($search) {
                $option['query'][] = [
                    'key' => 'sn',
                    'operator' => 'like',
                    'value' => "%{$search}%"
                ];
            }
            if ($process) {
                $process -= 1;
                $option['query'][] = [
                    'key' => 'process',
                    'operator' => 'like',
                    'value' => "%{$process}%"
                ];
            }
            $data = ContractService::getContractList($limit, $offset, $order, $option);

            $this->sendDataTable($data);
        });
    }

    public function userContractList(Request $request)
    {
        $this->sendGet(function() use($request){
            $data = ContractService::getUserContracts();

            $this->sendJson($data);
        });
    }

    public function contractDetail(Request $request)
    {
        $this->sendGet(function() use($request){
            $id = $request->get('id');
            $data = ContractService::getContractDetail($id);

            $this->sendJson($data);
        });
    }

    public function contractSave(Request $request)
    {
        $this->sendPost(function() use($request){
            $startEndTime = explode(' ~ ', $request->get('start_end_time'));
            $applyStartEndTime = explode(' ~ ', $request->get('apply_start_end_time'));
            $data = [
                'id' => $request->get('id'),
                'title' => $request->get('title'),
                'start_at' => $startEndTime[0],
                'end_at' => $startEndTime[1],
                'apply_start_at' => $applyStartEndTime[0],
                'apply_end_at' => $applyStartEndTime[1],
                'desc' => $request->get('desc'),
                'state' => $request->get('state'),
                'status' => $request->get('status')
            ];
            ContractService::saveContract($data);
            $this->sendJson([], 200, '更新成功');
        });
    }

    public function contractUpdate(Request $request)
    {
        $this->sendPost(function() use($request){
            $data = $request->get('data');
            $startEndTime = explode(' ~ ', $data['start_end_time']);

            ContractService::saveContract([
                'id' => $data['id'],
                'sn' => $data['sn'],
                'template_id' => $data['template_id'],
                'start_at' => $startEndTime[0],
                'end_at' => $startEndTime[1],
                'order_at' => $data['order_at'],
                'rent' => $data['rent'],
                'deposit' => $data['deposit'],
                'is_renewable' => $data['is_renewable'],
                'attachment' => $data['attachment']
            ]);
            $this->sendJson([], 200, '更新成功');
        });
    }

    public function contractDelete(Request $request)
    {
        $this->sendPost(function() use($request) {
            $id = $request->get('id');
            ContractService::deleteContract($id);
            $this->sendJson([], 200, "删除成功");
        });
    }

    public function changeContractProcess(Request $request)
    {
        $this->sendPost(function() use($request) {
            $data = $request->get('data');
            ContractService::changeContractProcess($data);
            $this->sendJson([], 200, "更新成功");
        });
    }

    public function templateList(Request $request)
    {
        $this->sendGet(function() use($request) {
            $limit = $request->get('limit');
            $offset = $request->get('offset');
            $order = $request->get('order');
            $data = ContractService::getTemplateList($limit, $offset, $order);
            $this->sendDataTable($data);
        });
    }

    public function contractTemplates(Request $request)
    {
        $this->sendGet(function() use($request) {
            $data = ContractService::getTemplates();

            $this->sendJson($data);
        });
    }

    public function templateDetail(Request $request)
    {
        $this->sendGet(function() use($request) {
            $id = $request->get('id');
            $data = ContractService::getTemplateDetail($id);
            $this->sendJson($data);
        });
    }

    public function saveTemplate(Request $request)
    {
        $this->sendPost(function() use($request) {
            $data = $request->get('data');
            ContractService::saveTemplate($data);
            $this->sendJson([], 200, "保存成功");
        });
    }

    public function upateTemplate(Request $request)
    {
        $this->sendPost(function() use($request) {
            $data = $request->get('data');
            ContractService::saveTemplate($data);
            $this->sendJson([], 200, "更新成功");
        });
    }

    public function deleteTemplate(Request $request)
    {
        $this->sendPost(function() use($request) {
            $id = $request->get('id');
            ContractService::deleteTemplate($id);
            $this->sendJson([], 200, "删除成功");
        });
    }

    public function userApart(Request $request, $apartId)
    {
        $this->sendGet(function() use($request, $apartId) {
            $limit = $request->get('limit');
            $offset = $request->get('offset');
            $order = $request->get('order');
            $data = ApartService::getUserApartList($limit, $offset, $order, $apartId);
            $this->sendDataTable($data);
        });
    }

    public function rentList(Request $request)
    {
        $this->sendGet(function() use($request){
            $limit = $request->get('limit');
            $offset = $request->get('offset');
            $order = $request->get('order');
            $status = $request->get('status');
            $search = $request->get('search');
            $option = [];
            if ($status) {
                $option['query'][] = [
                    'key' => 'status',
                    'operator' => '=',
                    'value' => $status
                ];
            }
            if ($search) {
                $apartIds = $this->searchApartIdsByName($search);
                if (count($apartIds)) {
                    $option['query'][] = [
                        'key' => 'apart_id',
                        'operator' => 'in',
                        'value' => $apartIds
                    ];
                }
            }
            $data = RentService::getRentList($limit, $offset, $order, $option);
            $this->sendDataTable($data);
        });
    }

    public function rentLast(Request $request)
    {
        $this->sendGet(function() use($request){
            $apartId = $request->get('apart_id');
            $userId = $request->get('user_id');
            $data = RentService::getRentLast($apartId, $userId);
            $this->sendJson($data);
        });
    }

    public function rentDetail(Request $request)
    {
        $this->sendGet(function() use($request){
            $id = $request->get('id');
            $data = RentService::getRentDetail($id);
            $this->sendJson($data);
        });
    }

    public function rentSave(Request $request)
    {
        $this->sendPost(function() use($request) {
            $data = $request->get('data');
            $startEndTime = explode(' ~ ', $data['start_end_time']);
            $data['start_at'] = $startEndTime[0];
            $data['end_at'] = $startEndTime[1];
            RentService:: saveRent($data);
            $this->sendJson([], 200, '添加成功');
        });
    }

    public function rentUpdate(Request $request)
    {
        $this->sendPost(function() use($request) {
            $data = $request->get('data');
            $startEndTime = explode(' ~ ', $data['start_end_time']);
            $data['start_at'] = $startEndTime[0];
            $data['end_at'] = $startEndTime[1];
            RentService:: saveRent($data);
            $this->sendJson([], 200, '修改成功');
        });
    }

    public function feeList(Request $request)
    {
        $this->sendGet(function() use($request) {
            $limit = $request->get('limit');
            $offset = $request->get('offset');
            $order = $request->get('order');
            $data = FeeService::getFeeList($limit, $offset, $order);

            $this->sendDataTable($data);
        });
    }

    public function feeTypeList(Request $request, $type)
    {
        $this->sendGet(function() use($request, $type) {
            $limit = $request->get('limit');
            $offset = $request->get('offset');
            $order = $request->get('order');
            $search = $request->get('search');
            $status = $request->get('status');
            $option = [];
            if ($type) {
                $option['query'][] = [
                    'key' => 'type',
                    'operator' => '=',
                    'value' => $type
                ];
            }
            if ($search) {
                $apartIds = $this->searchApartIdsByName($search);
                if (count($apartIds)) {
                    $option['query'][] = [
                        'key' => 'apart_id',
                        'operator' => 'in',
                        'value' => $apartIds
                    ];
                }
            }
            if ($status) {
                $option['query'][] = [
                    'key' => 'status',
                    'operator' => '=',
                    'value' => $status
                ];
            }
            $data = FeeService::getFeeList($limit, $offset, $order, $option);

            $this->sendDataTable($data);
        });
    }

    public function feeLast(Request $request)
    {
        $this->sendGet(function() use($request) {
            $type = $request->get('type');
            $apartId = $request->get('apart_id');
            $userId = $request->get('user_id');
            $data = FeeService::getFeeLast($type, $apartId, $userId);

            $this->sendJson($data);
        });
    }

    public function feeSave(Request $request)
    {
        $this->sendPost(function() use($request) {
            $data = $request->get('data');
            $startEndTime = explode(' ~ ', $data['start_end_time']);
            $data['start_at'] = $startEndTime[0];
            $data['end_at'] = $startEndTime[1];
            unset($data['start_end_time']);
            FeeService:: saveFee($data);
            $this->sendJson([], 200, '添加成功');
        });
    }

    public function feeDelete(Request $request)
    {
        $this->sendPost(function() use($request) {
            $id = $request->get('id');
            FeeService::deleteFee($id);
            $this->sendJson([], 200, '删除成功');
        });
    }

    public function changePayType(Request $request)
    {
        $this->sendPost(function() use($request) {
            $data = $request->get('data');
            FeeService::changePayType($data);
            $this->sendJson([], 200, '更新成功');
        });
    }

    public function changeRentPayType(Request $request)
    {
        $this->sendPost(function() use($request) {
            $data = $request->get('data');
            RentService::changePayType($data);
            $this->sendJson([], 200, '更新成功');
        });
    }

    public function retireList(Request $request)
    {
        $this->sendGet(function() use($request) {
            $limit = $request->get('limit');
            $offset = $request->get('offset');
            $order = $request->get('order');
            $type = $request->get('type');
            $search = $request->get('search');
            $option = [];
            if ($type) {
                $option['query'][] = [
                    'key' => 'type',
                    'operator' => '=',
                    'value' => $type
                ];
            }
            if ($search) {
                $apartIds = $this->searchApartIdsByName($search);
                if (count($apartIds)) {
                    $option['query'][] = [
                        'key' => 'apart_id',
                        'operator' => 'in',
                        'value' => $apartIds
                    ];
                }
            }
            $data = RetireService::getRetireList($limit, $offset, $order, $option);

            $this->sendDataTable($data);
        });
    }

    public function retireSave(Request $request)
    {
        $this->sendPost(function() use($request) {
            $data = $request->get('data');
            $data['created_by'] = $this->uid;
            RetireService::saveRetire($data);
            $this->sendJson([], 200, '更新成功');
        });
    }

    public function repairList(Request $request)
    {
        $this->sendGet(function() use($request){
            $limit = $request->get('limit');
            $offset = $request->get('offset');
            $order = $request->get('order');
            $status = $request->get('status');
            $search = $request->get('search');
            $option = [];
            if ($status) {
                $option['query'][] = [
                    'key' => 'status',
                    'operator' => '=',
                    'value' => $status
                ];
            }
            if ($search) {
                $apartIds = $this->searchApartIdsByName($search);
                if (count($apartIds)) {
                    $option['query'][] = [
                        'key' => 'apart_id',
                        'operator' => 'in',
                        'value' => $apartIds
                    ];
                }
            }
            $data = RepairService::getRepairList($limit, $offset, $order, $option);
            $this->sendDataTable($data);
        });
    }

    public function repairDetail(Request $request)
    {
        $this->sendGet(function() use($request) {
            $id = $request->get('id');
            $data = RepairService::getRepairDetail($id);
            $this->sendJson($data);
        });
    }

    public function repairDelete(Request $request)
    {
        $this->sendPost(function() use($request) {
            $id = $request->get('id');
            RepairService::deleteRepair($id);
            $this->sendJson([], 200, '删除成功');
        });
    }

    public function changeRepairStatus(Request $request)
    {
        $this->sendPost(function() use($request) {
            $data = $request->get('data');
            RepairService::changeRepairStatus($data);
            $this->sendJson([], 200, '操作成功');
        });
    }

    public function teacherList(Request $request)
    {
        $this->sendGet(function() use($request) {
            $limit = $request->get('limit');
            $offset = $request->get('offset');
            $order = $request->get('order');
            $search = $request->get('search');
            $status = $request->get('status');
            $option = [];
            if ($search) {
                $option['query'][] = [
                    'key' => 'realname',
                    'operator' => 'like',
                    'value' => "%{$search}%"
                ];
            }
            if ($status) {
                $option['query'][] = [
                    'key' => 'status',
                    'operator' => '=',
                    'value' => $status - 1
                ];
            }
            $data = UserService::getTeacherList($limit, $offset, $order, $option);
            $this->sendDataTable($data);
        });
    }

    public function teacherDetail(Request $request)
    {
        $this->sendGet(function() use($request) {
            $id = $request->get('id');
            $data = UserService::getTeacherDetail($id);
            $this->sendJson($data);
        });
    }

    public function teacherCheck(Request $request)
    {
        $this->sendPost(function() use($request) {
            $data = $request->get('data');
            UserService::checkTeacher($data);
            $this->sendJson([], 200, '审核完成');
        });
    }

    public function teacherDelete(Request $request)
    {
        $this->sendPost(function() use($request) {
            $id = $request->get('id');
            UserService::deleteTeacher($id);
            $this->sendJson([], 200, '删除成功');
        });
    }

    public function teacherScoreList(Request $request)
    {
        $this->sendGet(function() use($request) {
            $id = $request->get('id');
            $data = UserService::getTeacherScoreList($id);
            $this->sendJson($data);
        });
    }

    public function saveScore(Request $request)
    {
        $this->sendPost(function() use($request) {
            $data = $request->get('data');
            UserService::saveTeacherScore($data);
            $this->sendJson([], 200, '打分成功');
        });
    }

    public function retireDetail(Request $request)
    {
        $this->sendGet(function() use($request) {
            $id = $request->get('id');
            $contractId = $request->get('contract_id');
            $data = RetireService::getRetireDetail($id, $contractId);
            $this->sendJson($data);
        });
    }

    public function rentDelete(Request $request)
    {
        $this->sendPost(function() use($request){
            $id = $request->get('id');
            $data = RentService::deleteRent($id);
            $this->sendJson($data, 200, '删除成功');
        });
    }

    public function roleList(Request $request)
    {
        $this->sendGet(function() use($request) {
            $limit = $request->get('limit');
            $offset = $request->get('offset');
            $order = $request->get('order');
            $search = $request->get('search');
            $role = $request->get('role');
            $option = [];
            if ($search) {
                $option['query'][] = [
                    'key' => 'realname',
                    'operator' => 'like',
                    'value' => "%{$search}%"
                ];
            }
            if ($role) {
                $option['query'][] = [
                    'key' => 'role',
                    'operator' => '=',
                    'value' => $role
                ];
            }
            $data = UserService::getRoleList($limit, $offset, $order, $option);
            $this->sendDataTable($data);
        });
    }

    public function roleChange(Request $request)
    {
        $this->sendPost(function() use($request){
            $data = $request->get('data');
            UserService::changeUserRole($data);
            $this->sendJson([], 200, '修改角色成功');
        });
    }

    public function retireDelete(Request $request)
    {
        $this->sendPost(function() use($request){
            $id = $request->get('id');
            RetireService::deleteRetire($id);
            $this->sendJson([], 200, '删除成功');
        });
    }

    public function sendSms(Request $request)
    {
        $this->sendPost(function() use($request){
            $uid = $request->get('user_id');
            $flag = $request->get('flag');
            $from = User::where('id', $this->uid)->first();
            $to = User::where('id', $uid)->first();
            $title = '';
            $content = '';
            switch ($flag) {
                case 1 : $title = '审核通过';$content = '您的租房申请已通过,请进入选房阶段';break;
                case 2 : $title = '审核不通过';$content = '您的租房申请未通过,如需重新申请请即时申请';break;
                case 3 : $title = '选房成功';$content = '选房成功,请进入合同签订阶段';break;
                case 4 : $title = '选房不成功';$content = '选房不成功,请重新选房';break;
            }
            RetireService::sendSms($title, $content, $from, $to);
            $this->sendJson([], 200, '短信发送成功成功');
        });
    }

    public function apartImport(Request $request)
    {
        $this->sendPost(function() use($request){
            $file = $request->get('file');
            $data = \Import::importExcelToArray('/home/wwwroot/www.apartment.cn/public/files/'.$file);
            ApartService::import($data);
            $this->sendJson([]);
        });
    }
}



